# from appium import webdriver
# from time import sleep
# import random
#
# #1..连接设备，启动appimport time
#
# from appium.webdriver.common.touch_action import TouchAction#连接信息.[背下来的]
# desired_caps={
#     "deviceName": "Android Emulator",  # 设备名
#     "udid": "127.0.0.1:62001",  # 设备唯一标识.adb devices
#     "platformName":"Android",#手机系统
#     "platformVersion": "6.0.1",  # 手机版本
#     "appPackage": "com.mobivans.onestrokecharge", # 连接后启动的app包名
#     "appActivity": "com.stub.stub01.Stub01",#连接后启动app的类名
#     "unicodeKeyboard": True,  # 使用支持中文的输入法
#     "resetKeyboard": True  # 停止的时候重置输入法l
# }
# #2.连接
# # wd :表示的是webdriver协议—-- selenium用的也是这个。# hub :表示集线器，可以连接多个手机。
# driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub" ,desired_capabilities=desired_caps)#太快?以前selenium —--> sleep ,隐式等待，显示等待
# driver.implicitly_wait(10)#隐式等待10秒
# #3.一系列操作
# #问题1: xpath太长了
# driver.find_element_by_id("com.mobivans.onestrokecharge:id/main_write1").click()
# all_els=driver.find_elements_by_id("com.mobivans.onestrokecharge:id/item_cate_text")
# sleep(1)
# for e in all_els:
#     print(e.text)#拿元素的文本
#     # print(e.get_attribute("bounds"))#拿任何的属性
# random_el=random.choice(all_els)  # 随机选一个
# random_el.click()



import random
import time
from appium import webdriver
# 手势操作
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.wait import WebDriverWait
desired_caps = {
    "deviceName": "Android Emulator",  # 设备名
    "udid": "127.0.0.1:62001",  # 设备唯一标识   adb devices
    "platformName": "Android",  # 手机系统
    "platformVersion": "5.1.1",  # 手机版本
    "appPackage": "com.mobivans.onestrokecharge",  # 连接后启动的app包名
    "appActivity": "com.stub.stub01.Stub01",  # 连接后启动app的类名
    "unicodeKeyboard": True,  # 使用支持中文的输入法
    "resetKeyboard": True  # 停止的时候重置输入法
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_capabilities=desired_caps)
driver.implicitly_wait(10)
for i in range(5):
    el1 = driver.find_element_by_xpath("//android.widget.TextView[@text='记一笔']")
    el1.click()
    all_els = driver.find_elements_by_id("com.mobivans.onestrokecharge:id/item_cate_text")
    random_el = random.choice(all_els)  # 随机选一个
    random_el.click()
    el3 = driver.find_element_by_id("com.mobivans.onestrokecharge:id/keyb_btn_9")  # 金额
    el3.click()
    el4 = driver.find_element_by_id("com.mobivans.onestrokecharge:id/add_et_remark")  # 备注输入框
    el4.click()
    el4.send_keys("飞天茅台")
    el5 = driver.find_element_by_id("com.mobivans.onestrokecharge:id/keyb_btn_finish")  # 点击完成
    el5.click()
    TouchAction(driver).tap(x=68, y=88).perform()  # tap 表示按一下，取消登录弹窗

count_zhichu = driver.find_element_by_id('com.mobivans.onestrokecharge:id/account_txt_pay').text  # 总支出
if count_zhichu==f'{5*9}':
    print('金额计算正确')
else:
    print('金额计算不正确')

el6s=driver.find_elements_by_id("com.mobivans.onestrokecharge:id/account_item_txt_remark")  # 获取记账列表
el6 = random.choice(el6s)
TouchAction(driver).long_press(el6,3).perform()   # 在元素上按3秒。
el7=WebDriverWait(driver,20).until(lambda x:x.find_element_by_id("com.mobivans.onestrokecharge:id/alert_tv_ok"))  # 等待确认框
el7.click()

time.sleep(30)
driver.quit()


